//
//  EvaluateFooterView.m
//  BaseProject
//
//  Created by 王洋 on 2020/9/13.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "EvaluateFooterView.h"
#import "PostImgUploadCell.h"

#import <YYStarView.h>

@interface EvaluateFooterView ()<UITextViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) YYStarView *starView;

@end

@implementation EvaluateFooterView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self layout];
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    if ([textView.text isEqualToString:@"从多个角度评价宝贝，可以帮助更多想买的人~"]) {
        textView.text = @"";
    }
    
    if ([textView.text isEqualToString:@"从多个角度评价店铺，可以帮助更多需要服务的人~"]) {
        textView.text = @"";
    }
    
    if ([textView.text isEqualToString:@"从多个角度评价宝贝，可以帮助更多想买的人"]) {
        textView.text = @"";
    }
    
    
}

- (void)layout {
    
    self.contentTV.delegate = self;
    
    YYStarView *starView = [YYStarView new];
    starView.starBrightImageName = @"daxing_sel";
    starView.starDarkImageName = @"daxing_nor";
    starView.starSpacing = 10;
    starView.starScore = 5.0;
    
    WEAKSELF
    starView.starClick = ^ {
        if (weakSelf.EvaluateFooterViewActionBlock) {
            weakSelf.EvaluateFooterViewActionBlock(EvaluateFooterViewActionType_star, self.starView.starScore, 0, @"");
        }
    };
    
    [self.starBGView addSubview:starView];
    
    [starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    self.starView = starView;
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionview.collectionViewLayout = flowLayout;
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.showsHorizontalScrollIndicator = NO;
    self.collectionview.showsVerticalScrollIndicator = NO;

    [self.collectionview registerNib:[UINib nibWithNibName:@"PostImgUploadCell" bundle:nil] forCellWithReuseIdentifier:@"PostImgUploadCellID"];
    self.collectionview.backgroundColor = [UIColor clearColor];
    
}


- (BOOL)textViewShouldEndEditing:(UITextView *)textView {

    if (self.EvaluateFooterViewActionBlock) {
        self.EvaluateFooterViewActionBlock(EvaluateFooterViewActionType_content, 0.0f, 0, textView.text);
    }
    
    return YES;
}

- (void)fillFooterWithModel:(EvaluateOrderSubmitModel *)model {
    self.model = model;
    
    self.starView.starScore = model.ueEvaluateStar;
    
    
    self.starLab.text = [NSString stringWithFormat:@"%.1lf分",model.ueEvaluateStar];
    
    NSString *desc = @"非常差";
    if (model.ueEvaluateStar >= 2) {
        desc = @"差";
    }
    if (model.ueEvaluateStar >= 3) {
        desc = @"一般";
    }
    if (model.ueEvaluateStar >= 4) {
        desc = @"好";
    }
    if (model.ueEvaluateStar >= 5) {
        desc = @"非常好";
    }
    self.starLab.text = desc;
    
    self.contentTV.text = model.ueEvaluateContent;
    
    [self.collectionview reloadData];
}

#pragma mark collectionview

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PostImgUploadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PostImgUploadCellID" forIndexPath:indexPath];
    
    [cell fillCellWithModel:self.model.imageUrls[indexPath.row]];
    WEAKSELF
    cell.PostImgUploadCellBlock = ^(PostImgUploadCellActionType type) {
        
        switch (type) {
            case PostImgUploadCellActionType_del:{
                
                //删除图片的操作
                if (weakSelf.EvaluateFooterViewActionBlock) {
                    weakSelf.EvaluateFooterViewActionBlock(EvaluateFooterViewActionType_delPhoto, 0.0f, indexPath.row, @"");
                }
                
            } break;
            case PostImgUploadCellActionType_tap:{
                
                if (weakSelf.EvaluateFooterViewActionBlock) {
                    weakSelf.EvaluateFooterViewActionBlock(EvaluateFooterViewActionType_addPhoto, 0.0f, indexPath.row, @"");
                }
                
            } break;
            default:
                break;
        }
    };
    
    return cell;
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.imageUrls.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(110, 110);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}






@end
